package mymyoux.utils
{
	import flash.utils.Dictionary;
	import mymyoux.debug.Log;
	public dynamic class Hash extends Dictionary implements IMap
	{
		public function Hash(weakKeys:Boolean=false){
			super(weakKeys);
		}
		/* ajouts */
		public function put(key:Object,value:Object):void{
			this[key]=value;
		}		
		public function putAll(map:IMap):void{
			var r:Array=map.getKeys();
			for each(var i:String in r){
				this[i]=map.get(i);
			}
		}
		/* suppressions */
		public function remove(key:Object):void{
			this[key]=null;
			delete this[key];
		}
		public function removeValue(value:Object):void{
			if(containsValue(value)){
				for each(var i:Object in getKeys()){
					try{
						if(this[i].equals(value)){
							this[i]=null;
							if(!containsValue(value)){
								return;
							}
						}
					}catch(e:Error){
						if(this[i]===value){
							this[i]=null;
							if(!containsValue(value)){
								return;
							}
						}
					}
				}
			}
		}
		public function clear():void{
			for each(var i:Object in getKeys()){
				this[i]=null;
			}
		}
		/* tests */
		public function isEmpty():Boolean{
			for(var i:String in this){
				return false;
			}
			return true;
		}
		public function containsKey(key:Object):Boolean{
			return this[key]!=null;
		}
		public function containsValue(value:Object):Boolean{
			for each(var i:Object in this){
				try{
					if(i.equals(value)){
						return true;
					}
				}catch(e:Error){
					if(i===value){
						return true;
					}
				}
			}
			return false;
		}
		/* récupération */
		public function get(key:Object):Object{
			return this[key];
		}
		public function getKeys(value:Object=null):Array{
			//si value != null on ne renvoie que les clé ayant la valeur indiquée
			var r:Array=new Array();
			for(var i:String in this){
				if(this[i]!=null){
					try{
						if(value==null || this[i].equals(value)){
							r.push(i);
						}
					}catch(e:Error){
						if(value==null || this[i]===value){
							r.push(i);
						}					
					}
				}
			}
			return r;
		}
		public function getValues(unique:Boolean=false):Array{
			var r:Array=new Array();
			try{
			for(var i:String in this){;
				try{
				if(!unique || r.indexOf(this[i])==-1){
					r.push(this[i]);
				}
				}catch(e:Error){
					Log.error("ErrorValue : "+e);
				}
			}
			}catch(err:Error){
				Log.error("ErrorBigValue : "+err);
			}
			return r;
		}
		public function clone():Hash{
			var r:Hash=new Hash();
			r.putAll(this);
			return r;
		}
		public function get length():uint{
			return getKeys().length;
		}
	}
}